package com.tree.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tree.backend.common.Result;
import com.tree.backend.entity.dto.PowerEquipmentDTO;
import com.tree.backend.entity.pojo.PowerEquipment;
import com.tree.backend.entity.vo.PowerEquipmentVO;
import com.tree.backend.service.IPowerEquipmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


@RestController
@RequiredArgsConstructor
@RequestMapping("/powerEquipment")
public class PowerEquipmentController {
    private final IPowerEquipmentService powerEquipmentService;

    @GetMapping("/list")
    public Result<Page<PowerEquipmentVO>> getList(PowerEquipmentDTO powerEquipmentDTO) {
        return powerEquipmentService.getList(powerEquipmentDTO);
    }


    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(powerEquipmentService.removeById(id));
    }

    @PutMapping
    public Result<Boolean> update(@RequestBody PowerEquipmentDTO powerEquipmentDTO) {
        // 直接转换
        PowerEquipment powerEquipment = new PowerEquipment();
        BeanUtils.copyProperties(powerEquipmentDTO, powerEquipment);
        return Result.success(powerEquipmentService.updateById(powerEquipment));
    }

    @PostMapping
    public Result<Boolean> add(@RequestBody PowerEquipmentDTO powerEquipmentDTO) {
        PowerEquipment powerEquipment = new PowerEquipment();
        BeanUtils.copyProperties(powerEquipmentDTO, powerEquipment);
        return Result.success(powerEquipmentService.save(powerEquipment));
    }
}
